<?php
namespace Home\Controller;

use Home\Common\Common;

class PostinfoController extends Common {
    //岗位管理
    public function index(){
        $postinfo = M('postinfo');
        if (empty($_GET['name'])){
            $where = array('postinfo.state'=>1,'departmentinfo.state'=>1);
        }else{
            $where = array('`pt_name` like "%'.$_GET['name'].'%" or `dpt_name` like "%'.$_GET['name'].'%"','postinfo.state'=>1,'departmentinfo.state'=>1);
            $this->assign('name',$_GET['name']);
        }
        $count = $postinfo->where($where)->join('departmentinfo on postinfo.dpt_id=departmentinfo.dpt_id')->count();
        $page = new \Think\Page($count,6);
        $show = $page->show();
        $postinfo = $postinfo->where($where)->join('departmentinfo on postinfo.dpt_id=departmentinfo.dpt_id')->field('postinfo.*,departmentinfo.dpt_id,departmentinfo.dpt_name')->limit($page->firstRow.','.$page->listRows)->order('pt_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('postinfo',$postinfo);
        $this->display();
    }
    //添加岗位信息
    public function add(){
        if(empty($_POST['submit'])){
            $dptlist = M('departmentinfo');
            $dptlist = $dptlist->select();
            $this->assign('dptlist',$dptlist);
            $this->display();
        }else if(empty($_POST['pt_name']) || empty($_POST['pt_function'])){
            $this->error('请将信息填写完整',U('add'));
        }else{
            $pt = M('postinfo');
            if(!empty($_POST['pt_name']))
                $pt->add($_POST);
                $this->success('添加成功','index');
        }
    }
    //修改岗位信息
    public function edit(){
        if(empty($_POST['submit'])){
            if(empty($_GET['pt_id'])){
                $this->error('参数错误',U('index'));
            }
            $postinfo = M('postinfo');
            $dept = M('departmentinfo');
            $dept = $dept->select();
            $ptlist = $postinfo->where('`pt_id`='.$_GET['pt_id'])->find();
            $this->assign('ptlist',$ptlist);
            $this->assign('dept',$dept);
            $this->display();
        }else{
            $postinfo = M('postinfo');
            if(empty($_POST['pt_name']) || empty($_POST['pt_function'])){
                $this->error('请将信息填写完整',U('edit'));
            }
            if (!$postinfo->create('',2)){
                $this->error($postinfo->getError());
            }
            $postinfo->where('`pt_id`='.$_POST['pt_id'])->save();
            $this->success('修改成功',U('index'));
        }
    }
    //权限管理
    public function qx(){
        if (empty($_POST['submit'])){//判断是否有提交
            if (empty($_GET['pt_id'])) {//判断pt_id是否为空
                $this->error('参数错误', U('index'));
            }
            $menu = M('menu');//查询菜单表
            $menu_list = $menu->where('`hidden`=1')->select();
            $auth= M('auth');//查询权限表
            $auth_list = $auth->where('`state`=1 and `pt_id`='.$_GET['pt_id'])->getField('mid,state');
            $this->assign('menu_list',$menu_list);
            $this->assign('auth_list',$auth_list);
            $this->display();
        }else{
            $auth = M('auth');
            $auth->where('`pt_id`='.$_POST['pt_id'])->save(array('state'=>0));
            foreach ($_POST['mids'] as $mid){
                if($auth->where('`pt_id`='.$_POST['pt_id'].' and `mid`='.$mid)->find()){
                    $auth->where('`pt_id`='.$_POST['pt_id'].' and `mid`='.$mid)->save(array('state'=>1));
                }else{
                    $auth->add(array('pt_id'=>$_POST['pt_id'],'mid'=>$mid,'state'=>1));
                }
            }
            $this->success('权限设置成功',U('index'));
        }
    }
    //隐藏岗位信息
    public function del(){
        if (empty($_GET['pt_id']) && empty($_GET['state'])){
            $this->error('参数错误',U('index'));
        }else{
            $postinfo = M('postinfo');
            $_GET['state']=0;//修改状态为0（隐藏）
            $postinfo->save($_GET);
            $this->success('隐藏成功',U('index'));
        }
    }
    //批量隐藏
    public function del_all(){
        $postinfo = M('postinfo');
        if(empty($_GET['pt_ids'])){
            $this->error('请选择需要隐藏的内容',U('index'));
        }
        $pt_ids = implode(',',$_GET['pt_ids']);//获取id
        $str = explode(",",$pt_ids);//去逗号
        foreach ($str as $a){//循环id
            $postinfo->where(array('pt_id'=>$a))->save(array('state'=>0));
        }
        $this->success('隐藏成功',U('index'));
    }
    public function pt_view(){
        $postinfo = M('postinfo');
        if (empty($_POST['name'])){
            $where = array('postinfo.state'=>0);
        }else{
            $where = array('`pt_name` like "%'.$_POST['name'].'%" or `dpt_name` like "%'.$_POST['name'].'%"','postinfo.state'=>0);
            $this->assign('name',$_POST['name']);
        }

        $count = $postinfo->where($where)->join('departmentinfo on postinfo.dpt_id=departmentinfo.dpt_id')->count();
        $page = new \Think\Page($count,6);
        $show = $page->show();
        $postinfo = $postinfo->where($where)->join('departmentinfo on postinfo.dpt_id=departmentinfo.dpt_id')->field('postinfo.*,departmentinfo.dpt_id,departmentinfo.dpt_name')->limit($page->firstRow.','.$page->listRows)->order('pt_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('postinfo',$postinfo);
        $this->display();
    }
    //显示岗位信息
    public function xs(){
        if (empty($_GET['pt_id']) && empty($_GET['state'])){
            $this->error('参数错误',U('pt_view'));
        }else{
            $postinfo = M('postinfo');
            $_GET['state']=1;//修改状态为1（显示）
            $postinfo->save($_GET);
            $this->success('显示成功',U('index'));
        }
    }
    //批量显示
    public function xs_all(){
        $postinfo = M('postinfo');
        if(empty($_GET['pt_ids'])){
            $this->error('请选择需要显示的内容',U('pt_view'));
        }
        $pt_ids = implode(',',$_GET['pt_ids']);//获取id
        $str = explode(",",$pt_ids);//去逗号
        foreach ($str as $a){//循环id
            $postinfo->where(array('pt_id'=>$a))->save(array('state'=>1));
        }
        $this->success('显示成功',U('index'));
    }
    //删除岗位信息
    public function delete(){
        if (empty($_GET['pt_id']) && empty($_GET['state'])){
            $this->error('参数错误',U('index'));
        }else{
            $postinfo = M('postinfo');
            $postinfo->where('pt_id='.$_GET['pt_id'])->delete($_GET);
            $this->success('删除成功',U('index'));
        }
    }
}